<?php
/**
 * Index
 * Created by Veyron
 * motto:现在的拼搏 为了更好的明天
 * day:  2020/3/26
 * Time: 22:29
 */

namespace app\api\controller\order;

use app\api\controller\AuthBase;
use app\common\business\Order;
use app\common\lib\Show;

class Index extends AuthBase
{
    /**
     * 创建订单
     * @return \think\response\Json
     */
    public function save(){
        $addressId = input('param.address_id','0','intval');
        $ids       = input('param.ids','0','trim');
        if(!$ids || !$addressId){
            return Show::error('参数不合法');
        }

        $data = [
            'ids'           => $ids,
            'address_id'    => $addressId,
            'user_id'       => $this->userId
        ];
        try {
            $result = (new Order())->save($data);
        }catch (\Exception $e){
            return Show::error($e->getMessage());
        }
        if(!$result){
            return Show::error('订单提交失败,请稍候重试');
        }
        return Show::success($result);
    }
    public function read(){
        $id = input('param.id', '','intval');
        if(empty($id)){
            return Show::error('参数不合法');
        }
        $data = [
            'user_id'   => $this->userId,
            'order_id'  => $id
        ];
        $result = (new Order())->detail($data);
        if(!$result){
            return Show::error('获取订单失败');
        }
        return Show::success($result);

    }
}

